#include "sound.h"

Sound::Sound()
{
	buffer = 0;
	source = 0;
}

Sound::~Sound()
{
	//buffer = 0;
	//source = 0;
}

void Sound::Load(char* filename, ALfloat pitch, ALfloat gain, ALfloat* sourcePos, ALfloat* sourceVel, ALboolean loop)
{
	ALenum format;
	ALsizei size;
	ALvoid* data;
	ALsizei frequency;
	ALboolean unused;

	alGenBuffers(1, &buffer);
	alutLoadWAVFile(filename, &format, &data, &size, &frequency, &unused);
	alBufferData(this->buffer, format, data, size, frequency);
	alutUnloadWAV(format, data, size, frequency);

	alGenSources(1, &source);
	alSourcei	(this->source, AL_BUFFER, this->buffer);
	alSourcef	(this->source, AL_PITCH, pitch);
	alSourcef	(this->source, AL_GAIN, gain);
	alSourcefv	(this->source, AL_POSITION, sourcePos);
	alSourcefv	(this->source, AL_VELOCITY, sourceVel);
	alSourcei	(this->source, AL_LOOPING, loop);
}

void Sound::Release()
{
	alDeleteBuffers(1, &buffer);
	alDeleteSources(1, &source);
}

void Sound::Play()
{
	alSourcePlay(this->source);
}

void Sound::Pause()
{
	alSourcePause(this->source);
}

void Sound::Stop()
{
	alSourceStop(this->source);
}

bool Sound::IsPlaying()
{
	ALenum state;
	alGetSourcei(this->source, AL_SOURCE_STATE, &state);
	return (state == AL_PLAYING);
}

void Sound::SetListenerPosition(ALfloat* position)
{
	alListenerfv(AL_POSITION, position);
}

void Sound::SetListenerVelocity(ALfloat* velocity)
{
	alListenerfv(AL_VELOCITY, velocity);
}

void Sound::SetListenerOrientation(ALfloat* orientation)
{
	alListenerfv(AL_ORIENTATION, orientation);
}